<template>
  <div class="header-container">
    <div class="header-left flex-box">
      <!-- 组合式api点击采用commit去触发 https://vuex.vuejs.org/zh/guide/composition-api.html#%E8%AE%BF%E9%97%AE-mutation-%E5%92%8C-action -->
      <el-icon class="icon" size="20" @click="toggleCollapse">
        <Fold />
      </el-icon>
      <!-- 这个class的意思是满足后面的条件就展示前面这个样式 -->
      <ul class="flex-box">
        <li
          v-for="(item,index) in selectMenu"
          :key="item.path"
          :class="{ selected: route.path === item.path }"
          class="tab flex-box"
        >
          <el-icon size="20">
            <component :is="item.icon" />
          </el-icon>
          <!-- 同理侧边栏和上边栏点击都需要跳转页面 -->
          <router-link class="text flex-box" :to="{ path: item.path }">
            {{ item.name }}
          </router-link>
          <el-icon size="20" class="close" @click="closeTab(item, index)">
            <Close />
          </el-icon>
        </li>
      </ul>
    </div>

    <div class="header-right">
      <!-- https://element-plus.youqua.cn/zh-CN/component/dropdown.html
        下拉别表 -->
      <el-dropdown>
        <div class="el-dropdown-link flex-box">
          <!-- https://element-plus.youqua.cn/zh-CN/component/avatar.html -->
          <el-avatar
            src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
          />
          <p class="user-name">admin</p>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>Action 1</el-dropdown-item>
            <el-dropdown-item>Action 2</el-dropdown-item>
            <el-dropdown-item>Action 3</el-dropdown-item>
            <el-dropdown-item disabled>Action 4</el-dropdown-item>
            <el-dropdown-item divided>Action 5</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
// 侧边栏和上边栏联动
import { computed } from "vue";
import { useStore } from "vuex";
// 因为要展示点击模块高亮效果需要拿到当前页面关联的路由
import { useRoute,useRouter } from "vue-router";
import { Close, Fold } from "@element-plus/icons-vue";

// 拿到store对象,因为此时是需要去做侧边栏组件和上边栏组件联动,所以这个地方使用了组合式api
// https://vuex.vuejs.org/zh/guide/composition-api.html
const store = useStore();
const route = useRoute();
const router = useRouter();

// 定义点击按钮时触发的函数
const toggleCollapse = () => {
  store.commit("menu/collapseMenu"); // 提交mutation
};

const selectMenu = computed(() => store.state.menu.selectMenu);
console.log("this is");
console.log("selectMenu", selectMenu);

// 定义关闭逻辑，如果关闭后面还有元素就显示后一个，如果关闭是最后一个就显示前一个，如果都么有就显示首页
const closeTab = (item, index) => {
  store.commit("menu/closeMenu", item);
  // 删除非当前页面
  if (route.path !== item.path) {
    return;
  }
  const selectMenuData = selectMenu.value;
  //删除最后一项
  if(index === selectMenuData.length){

    if(!selectMenuData.length){
      router.push('/')
    }else{
      router.push({
        path:selectMenuData[index-1].path
      })
    }
  }else{
    router.push({
      path:selectMenuData[index].path
    })
  }
};
</script>

<style lang="less" scoped>
.flex-box {
  display: flex;
  align-items: center;
  height: 100%;
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  background-color: #fff;

  .header-left {
    height: 100%;
    .icon {
      width: 45px;
      height: 100%;
    }
    /* 用于处理背景变暗,且鼠标变成小手 */
    .icon:hover {
      background-color: #f5f5f5;
      cursor: pointer;
    }
    .tab {
      padding: 0 10px;
      height: 100%;
      .text {
        margin: 0 5px;
        font-size: 15px; /* 控制字体大小 */
      }
      // 用于做关闭显示
      .close {
        visibility: hidden;
      }
      // &符号代表同级别
      &.selected {
        a {
          color: #409eff;
        }
        i {
          color: #409eff;
        }
        background-color: #f5f5f5;
      }
    }
    .tab:hover {
      background-color: #f5f5f5;
      .close {
        visibility: inherit;
        cursor: pointer;
        color: #000;
      }
    }
  }

  .header-right {
    .el-dropdown-link {
      height: 100%;
      .user-name {
        margin-left: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 42.16px; /* 设置高度 */
        line-height: 42.16px; /* 设置行高，确保文字垂直居中 */
      }
    }
  }

  .a-tab {
    height: 100%;
    color: #333;
    font-size: 15px; /* 控制字体大小 */
  }
}
</style>
